//
//  ECHTTPParam.m
//  SXBPurchase
//
//  Created by huanggulong on 2017/10/8.
//  Copyright © 2017年 历山大亚. All rights reserved.
//

#import "GLHTTPParam.h"
#import "MJExtension.h"

@interface GLHTTPParam ()

@property(nonatomic , strong)NSMutableDictionary *  otherDict;

@end

@implementation GLHTTPParam

+ (NSArray *)mj_ignoredPropertyNames{
    return @[@"otherDict"]; //忽略掉otherDict 这个属性
}

+ (instancetype)param{
    return [[self alloc] init];
}

- (NSString *)stringWithDictOrArray:(id)json{
    NSData *data=[NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSString *mm = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@" "];//除掉换行
    mm = [mm stringByReplacingOccurrencesOfString:@" " withString:@""];//除掉空格
    return mm;
}

- (void)setSafeValue:(id)value forKey:(NSString *)key{
    if (value == nil || key == nil) {
        NSLog(@"value(or key) is null ");
        return;
    }
    if ([value isKindOfClass:[NSString class]]) {
        [self.otherDict setObject:value forKey:key];
    }else if ([value isKindOfClass:[NSArray class]]  || [value isKindOfClass:[NSDictionary class]]){
        //将其改编为字符串
        NSString *newValue = [self stringWithDictOrArray:value];
        [self.otherDict setObject:newValue forKey:key];
    }else if([value isKindOfClass:[NSNumber class]]){
        [self.otherDict setObject:value forKey:key];
    }
}

- (void)addNullKeys:(NSArray<NSString *> *)keys{
    for (NSString * str in keys) {
        [self.otherDict setObject:[NSNull null] forKey:str];
    }
}

- (NSDictionary *)gl_keyValue{
    if (_otherDict) {
        NSMutableDictionary *dict = [self mj_keyValues];
        if (dict == nil) {
            dict = [NSMutableDictionary dictionary];
        }
        [dict addEntriesFromDictionary:_otherDict];
        return dict;
    }else{
        return [self mj_keyValues];
    }
}


- (NSMutableDictionary *)otherDict{
    if (_otherDict == nil) {
        _otherDict = [NSMutableDictionary dictionary];
    }
    return _otherDict;
}

@end
